var app = getApp(), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nick_name: '',
        isLoading: true,
        product_id: 0,
        product: [],
        productPrice: 0,
        order_id: 0,
        order: {},
        address: {},
        is_has_address: 0,
        address_id: 0,
        useTotalAmount: 0,
        totalAmount: 0,
        payPrice: 0,
        is_has_vip:0,
        selfVip:{},
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('validateCoupon options', options)
        var order_id = parseInt(options.order_id)
        this.onHttpInit(order_id)
    },
    onHttpInit: function (order_id) {
        var that = this
        fetchApi.httpPost('/product/order/detail', {
            order_id: order_id,
        }, function (res) {
            console.log("获取订单详情", res)
            if (res.data.code == 0) {
                if (res.data.data.is_has_address == 1) {
                    that.data.address_id = res.data.data.address.id;
                }
                that.data.order_id = res.data.data.order.id;
                that.data.address = res.data.data.address;
                that.setData({
                    is_has_vip: res.data.data.is_has_vip,
                    selfVip: res.data.data.selfVip,
                    order: res.data.data.order,
                    is_has_address: res.data.data.is_has_address,
                    address: res.data.data.address,
                    payPrice: res.data.data.order.payPrice,
                })
            } else {
                Toast.fail(res.data.msg)
            }
            that.setData({
                netWork: true,
                isLoading: false,
            })
        }, function () {
            that.setData({
                netWork: false,
                hasData: false,
                isLoading: false,
            })
        })
    },
    onShow: function (options) {
        this.defaultAddress();
    },
    cashCountChange(e) {
        console.log('cashCountChange', e)
    },
    onHttpGetCouponDetail: function (user_coupon_id) {
        var that = this
        fetchApi.httpPost('/user/coupon/detail', {
            user_coupon_id: user_coupon_id,
        }, function (res) {
            console.log("获取优惠券数据", res)
            if (res.data.code == 0) {
                var coupon_type = res.data.data.coupon.couponType
                if (coupon_type == 2) { //直接抵扣 按次使用的
                    that.setData({
                        productPrice: 0
                    })
                }
            }
            that.setData({
                netWork: true,
                isLoading: false,
            })
        }, function () {
            that.setData({
                netWork: false,
                hasData: false,
                isLoading: false,
            })
        })
    },
    onClickCouponList: function () {
        wx.navigateTo({
            url: '/pages/coupon/canuse/index?product_id=' + this.data.product_id,
        })
    },
    submitOrder: function (res) {
        var orderId = res.currentTarget.dataset.orderId;
        console.log('orderId', orderId);
        // if (this.data.address_id == 0) {
        //     Toast.fail("请选择收货地址");
        //     return;
        // }else{
        // }
        this.requestPay(this.data.order_id);
    },
    requestPay: function (order_id) {
        var that = this
        fetchApi.httpPost('/pay/requestPay', {
            address_id: that.data.address_id,
            order_id: order_id,
            subject: '购买商品',
        }, function (res) {
            console.log('提交订单', res)
            if (res.data.code == 0) {
                var pay_data = res.data.data;
                var wxpayinfo = JSON.parse(res.data.data)
                console.log('pay_data', pay_data)
                if (wxpayinfo.result_code == 'SUCCESS') {
                    wx.requestPayment({
                        timeStamp: wxpayinfo.timeStamp,
                        nonceStr: wxpayinfo.nonceStr,
                        package: 'prepay_id=' + wxpayinfo.prepay_id,
                        signType: wxpayinfo.signType,
                        paySign: wxpayinfo.paySign,
                        success(res) {
                            console.log('requestPayment success', res)
                            wx.redirectTo({
                                url: '/pages/pay/payResult/index?order_id=' + order_id,
                            })
                        },
                        fail(res) {
                            console.log('requestPayment fail', res)
                        }
                    })
                }
            } else {
                Toast.fail(res.data.msg);
                that.setData({
                    isLoading: false,
                    loading: true,
                    netWork: true
                })
            }
        }, function () {
            that.setData({
                isLoading: false,
                loading: true,
                netWork: true,
            })
        })
    },
    defaultAddress: function () {
        var that = this
        fetchApi.httpPost('/product/order/defaultAddress', {}, function (res) {
            console.log("获取默认地址", res);
            if (res.data.code == 0) {
                if (res.data.data) {
                    that.data.address = res.data.data;
                    that.data.address_id = res.data.data.id;
                    that.setData({
                        address: res.data.data,
                        is_has_address: 1
                    })
                }
            }
        }, function () {})
    },
})